package com.work.aspect;

import com.work.common.ctx.DataPermissionCtx;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class DataPermissionAspect {

    @Pointcut("@annotation(com.work.common.annotation.DataPermission)||@within(com.work.common.annotation.DataPermission)")
    public void dataPermission() {
    }

    @Before("dataPermission()")
    public void before(JoinPoint joinPoint) {
        //        TODO
        DataPermissionCtx.setDataPermission("xxxx");
        log.info("获取注解并且添加到上下文，通过拦截器修改sql实现数据权限");
    }

    @AfterReturning("dataPermission()")
    public void afterReturning() {
        //        TODO
        DataPermissionCtx.clear();
        log.info("移除上下文，避免内存泄漏");
    }

    @AfterThrowing("dataPermission()")
    public void afterThrowing() {
//        TODO
        DataPermissionCtx.clear();
        log.info("移除上下文，避免内存泄漏，如果报错的时候不移除上下文也会产生内存泄漏");
    }
}
